eXo Groovy Console

Description:

eXo Groovy Console allows you to run Groovy code interactively. It accepts eXo API and can access eXo components deployed in portal container so this can be used as a tool for drafting code, tesing or exploring eXo API/components/data interactively on a live system.

In a session, variables and import statements are saved automatically as session context for user to reference later if needed. Currently class and function definitions are not saved (due to the difficulty of parsing them from a script), however we can send them along with other statements to the console once at a time (as a single command in the console via copy/paste, or as a script)

Available session management commands:
 show imports                            Show imported packages
show variables Show defined variables
list sessions [<regex pattern>] List all saved sessions which's name matches <regex pattern>
clear session Clear the session
save session as <session name> Save session
load session <session name> Load session
remove session <session name> Remove session
remove all sessions [<regex pattern>] Remove all session which's name matches <regex pattern>

The console's GUI:

We use the JQuery Terminal Emulator plugin for the console's GUI. Below are some useful shotcuts and commands for working with the console (please refer to the plugin's website for more information):
Shotcut keys:
 Up/Down                                 Command history
 PgUp/PgDn                               Scroll up/down
 Ctrl-C/Ctrl-V                           Copy/Paste
 Ctrl-K                                  Remove the text after the cursor
 ESC                                     Clear command line
 
Console commands:
 clear                                   Clear console screen
 edit                                    Show extent editor
 pause                                   Disable terminal
 run                                     Execute content in extent input

Sample sessions:

1. Test importing:

groovy> println PI
ERROR: Error executing script: No such property: PI for class: Script1
groovy> import static java.lang.Math.*
groovy> println PI
3.141592653589793
groovy> println sin(PI/2)
1.0
groovy>

2. Test running eXo API and session management commands:

Run the following script line-by-line
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.Repository;
import javax.jcr.Session;

import org.exoplatform.container.ExoContainerContext;
import org.exoplatform.container.StandaloneContainer;
import org.exoplatform.services.jcr.RepositoryService;

RepositoryService repositoryService = (RepositoryService)ExoContainerContext.getCurrentContainer().getComponentInstanceOfType(RepositoryService.class);
Repository repository = repositoryService.getDefaultRepository();
Session session = repository.login();

Node rootNode = session.getRootNode();

println rootNode

rootNode.getNodes().each{println it.name}

The session:
groovy> import javax.jcr.Node;
groovy> import javax.jcr.Property;
groovy> import javax.jcr.Repository;
groovy> import javax.jcr.Session;
groovy> import org.exoplatform.container.ExoContainerContext;
groovy> import org.exoplatform.container.StandaloneContainer;
groovy> import org.exoplatform.services.jcr.RepositoryService;
groovy>
groovy> show imports
[GroovySession@2011.5.16_7h53m4s]
import org.exoplatform.services.jcr.RepositoryService;
import javax.jcr.Node;
import org.exoplatform.container.ExoContainerContext;
import org.exoplatform.container.StandaloneContainer;
import javax.jcr.Repository;
import javax.jcr.Session;
import javax.jcr.Property;

groovy> RepositoryService repositoryService = (RepositoryService)ExoContainerContext.getCurrentContainer().getComponentInstanceOfType(RepositoryService.class);
groovy> Repository repository = repositoryService.getDefaultRepository();
groovy> Session session = repository.login();
groovy> Node rootNode = session.getRootNode();
groovy>
groovy> show variables
[GroovySession@2011.5.16_7h53m4s]
repositoryService = (RepositoryService)ExoContainerContext.getCurrentContainer().getComponentInstanceOfType(RepositoryService.class);
repository = repositoryService.getDefaultRepository();
session = repository.login();
rootNode = session.getRootNode();

groovy>
groovy> println rootNode
org.exoplatform.services.jcr.impl.core.NodeImpl@145f173
groovy>
groovy> rootNode.getNodes().each{println it.name}
exo:registry
exo:applications
exo:services
Users
Groups
Digital Assets
Documents
Trash
Records Space
Application Data
tags
sites content
xcmis:system
exo:loginHistoryHome
exo:groovyConsoleService
groovy>
groovy> save session as JcrTest
Session was saved successfully to 'JcrTest'
groovy>
groovy> clear session
Session cleared.
groovy>
groovy> show imports
[GroovySession@2011.5.16_7h53m4s]
<empty>
groovy> show variables
[GroovySession@2011.5.16_7h53m4s]
<empty>
groovy> load session JcrTest
Session 'JcrTest' was loaded successfully.
groovy>
groovy> show imports
[GroovySession@2011.5.16_7h53m4s]
import org.exoplatform.services.jcr.RepositoryService;
import javax.jcr.Node;
import org.exoplatform.container.ExoContainerContext;
import org.exoplatform.container.StandaloneContainer;
import javax.jcr.Repository;
import javax.jcr.Session;
import javax.jcr.Property;

groovy> show variables
[GroovySession@2011.5.16_7h53m4s]
repositoryService = (RepositoryService)ExoContainerContext.getCurrentContainer().getComponentInstanceOfType(RepositoryService.class);
repository = repositoryService.getDefaultRepository();
session = repository.login();
rootNode = session.getRootNode();

groovy> println rootNode
org.exoplatform.services.jcr.impl.core.NodeImpl@1d29612
groovy>

3. Test running function and class definition via copy/paste

Copy and paste the following script to the console (use Ctrl-V to paste)
class myClass {
String hello() {
return "Hello from class"
}
}

myFunc()
cls = new myClass()
println cls.hello()

The session:

groovy> void myFunc() {
  println "Hello from function"
}

class myClass {
  String hello() {
    return "Hello from class"
  }
}

myFunc()
cls = new myClass()
println cls.hello()
Hello from function
Hello from class
groovy>